package com.ghaoxiang.servicemiya;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import brave.Tracer;
import brave.propagation.CurrentTraceContext;
import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController

public class ServiceMiyaApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceMiyaApplication.class, args);
	}

	private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());

	static ExecutorService executor = Executors.newFixedThreadPool(3);


	@RequestMapping("/hi")
	public String home(){
		System.out.println(CurrentTraceContext.Default.inheritable().get().traceId());

		LOG.log(Level.INFO, "hi is being called");
		return "hi i'm miya!";
	}

	@RequestMapping("/miya")
	public String info()throws Exception{
		LOG.log(Level.INFO, "info is being called");
//		System.out.println(tracer.currentSpan().context().traceId());
		System.out.println(CurrentTraceContext.Default.inheritable().get().traceId());
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				restTemplate.getForObject("http://localhost:8988/info",String.class);
			}
		});
		CurrentTraceContext.Default.inheritable().executorService(executor).execute(thread);

		Thread.currentThread().sleep(2000);
		return "xx2";
	}

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

	@Bean
	public Sampler defaultSampler() {
		return Sampler.ALWAYS_SAMPLE;
	}
}

